/**
 * 
 */
package nus.mtech.presentation;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;

import nus.mtech.ApplicationFacade;
import nus.mtech.constant.PresentationConstant;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

/**
 * @author Alvin Lee
 * 
 */
final class ViewBillPanel extends JPanel implements ActionListener {

	private static volatile ViewBillPanel INSTANCE;
	private Dimension screen = null;
	private JButton btnClear;
	private JButton btnSearch;
	private JComboBox comboBoxMonth;
	private JComboBox comboBoxYear;
	private JTextArea txtBill;
	private JTextField txtAccountNo;
	private JScrollPane scrollPane;
	private JButton btnGenerate;
	private JComboBox monthlyBillComboBoxMonth;
	private JComboBox monthlyBillComboBoxYear;
	private JButton monthlyBillGenerate;

	ViewBillPanel() {
		super(null);
		this.setBackground(PresentationConstant.CONSOLE_BACKGROUND);
		this.setPreferredSize(new Dimension(1004, 643));
		initialization();

	}

	public void initialization() {
		try {
			removeAll();

			screen = Toolkit.getDefaultToolkit().getScreenSize();

			final JLabel customerPanelLabel = new JLabel();
			customerPanelLabel.setHorizontalAlignment(SwingConstants.CENTER);
			customerPanelLabel.setFont(new Font("", Font.BOLD, 16));
			customerPanelLabel.setText("Bills");
			customerPanelLabel.setBounds(0, 10, 1004, 21);
			add(customerPanelLabel);
			
			JLabel borderBase = new JLabel();
			borderBase.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
			borderBase.setBounds(40, 71, 944, 661);
			add(borderBase);

			JLabel monthlyBillLabel = new JLabel("Generate monthly bill");
			monthlyBillLabel.setFont(new Font("Dialog", Font.BOLD, 14));
			monthlyBillLabel.setBounds(90, 88, 242, 21);
			add(monthlyBillLabel);
			
			JLabel monthlyBillYear = new JLabel("Year:");
			monthlyBillYear.setBounds(90, 133, 106, 14);
			add(monthlyBillYear);

			JLabel monthlyBillMonth = new JLabel("Month:");
			monthlyBillMonth.setBounds(206, 133, 106, 14);
			add(monthlyBillMonth);
			
			
			monthlyBillComboBoxYear = new JComboBox();
			monthlyBillComboBoxYear.setBounds(90, 158, 95, 20);
			monthlyBillComboBoxYear.addItem("");
			monthlyBillComboBoxYear.addItem("2011");
			monthlyBillComboBoxYear.addItem("2010");
			add(monthlyBillComboBoxYear);
			
			monthlyBillComboBoxMonth = new JComboBox();
			monthlyBillComboBoxMonth.setBounds(206, 158, 106, 20);
			monthlyBillComboBoxMonth.addItem("");
			monthlyBillComboBoxMonth.addItem("1");
			monthlyBillComboBoxMonth.addItem("2");
			monthlyBillComboBoxMonth.addItem("3");
			monthlyBillComboBoxMonth.addItem("4");
			monthlyBillComboBoxMonth.addItem("5");
			monthlyBillComboBoxMonth.addItem("6");
			monthlyBillComboBoxMonth.addItem("7");
			monthlyBillComboBoxMonth.addItem("8");
			monthlyBillComboBoxMonth.addItem("9");
			monthlyBillComboBoxMonth.addItem("10");
			monthlyBillComboBoxMonth.addItem("11");
			monthlyBillComboBoxMonth.addItem("12");
			add(monthlyBillComboBoxMonth);
			
			
			monthlyBillGenerate = new JButton("Generate");
			monthlyBillGenerate.setBounds(323, 158, 89, 23);
			monthlyBillGenerate.addActionListener(this);
			add(monthlyBillGenerate);
			
			JLabel lblComplaintInfo = new JLabel("Search Bill");
			lblComplaintInfo.setFont(new Font("Dialog", Font.BOLD, 14));
			lblComplaintInfo.setBounds(90, 188, 242, 21);
			add(lblComplaintInfo);
			
			JLabel lblAcountNo = new JLabel("Account No:");
			lblAcountNo.setBounds(90, 233, 106, 14);
			add(lblAcountNo);
			
			JLabel lblYear = new JLabel("Year:");
			lblYear.setBounds(339, 233, 106, 14);
			add(lblYear);

			JLabel lblMonth = new JLabel("Month:");
			lblMonth.setBounds(455, 233, 106, 14);
			add(lblMonth);
			
			txtAccountNo = new JTextField();
			txtAccountNo.setBounds(90, 258, 200, 20);
			add(txtAccountNo);
			
			btnSearch = new JButton("Search");
			btnSearch.setBounds(572, 258, 89, 23);
			btnSearch.addActionListener(this);
			add(btnSearch);
			
			txtAccountNo.setColumns(10);
			comboBoxYear = new JComboBox();
			comboBoxYear.setBounds(339, 258, 95, 20);
			comboBoxYear.addItem("");
			comboBoxYear.addItem("2011");
			comboBoxYear.addItem("2010");
			add(comboBoxYear);
			
			comboBoxMonth = new JComboBox();
			comboBoxMonth.setBounds(455, 258, 106, 20);
			comboBoxMonth.addItem("");
			comboBoxMonth.addItem("1");
			comboBoxMonth.addItem("2");
			comboBoxMonth.addItem("3");
			comboBoxMonth.addItem("4");
			comboBoxMonth.addItem("5");
			comboBoxMonth.addItem("6");
			comboBoxMonth.addItem("7");
			comboBoxMonth.addItem("8");
			comboBoxMonth.addItem("9");
			comboBoxMonth.addItem("10");
			comboBoxMonth.addItem("11");
			comboBoxMonth.addItem("12");
			add(comboBoxMonth);

			txtBill = new JTextArea();
            scrollPane = new JScrollPane(txtBill);
			scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
			scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
			scrollPane.setBounds(90, 310, 763, 333);
			add(scrollPane);
			
			btnClear = new JButton("Clear");
			btnClear.setBounds(342, 674, 89, 23);
			btnClear.addActionListener(this);
			add(btnClear);







				

		} catch (Exception ex) {
			System.out.println("exception");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent ae) {
		// TODO Auto-generated method stub
		if (ae.getSource() == btnClear) {
			clear();			
		} else if (ae.getSource() == btnSearch) {
			String accountNo = txtAccountNo.getText();
			if (accountNo.length() == 0) {
				JOptionPane.showMessageDialog(this,
						"Please enter Account No",
						"Account No Required.",
						JOptionPane.ERROR_MESSAGE);
			}
			else if ((comboBoxYear.getSelectedIndex() == 0) || (comboBoxMonth.getSelectedIndex() == 0)) {
				JOptionPane.showMessageDialog(this,
						"Please select Year and Month",
						"Year and Month Required.",
						JOptionPane.ERROR_MESSAGE);
				return;
			}
			else{
				String year = (String)comboBoxYear.getSelectedItem();
				String month = (String)comboBoxMonth.getSelectedItem();
				Calendar cal = Calendar.getInstance();
				cal.set(Calendar.YEAR,Integer.parseInt(year));
				cal.set(Calendar.MONTH, Integer.parseInt(month) -1 );
				cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
				String statement = ApplicationFacade.getInstance().getBillStatement(accountNo, cal.getTime());
				if(statement == null){
					txtBill.setText("Bill not ready");
				}
				else{
					txtBill.setText(statement);
				}
			}
		} else if(ae.getSource() == monthlyBillGenerate){
			if ((monthlyBillComboBoxYear.getSelectedIndex() == 0) || (monthlyBillComboBoxMonth.getSelectedIndex() == 0)) {
				JOptionPane.showMessageDialog(this,
						"Please select Year and Month",
						"Year and Month Required.",
						JOptionPane.ERROR_MESSAGE);
				return;
			}
			String year = (String)monthlyBillComboBoxYear.getSelectedItem();
			String month = (String)monthlyBillComboBoxMonth.getSelectedItem();
			Calendar cal = Calendar.getInstance();
			cal.set(Calendar.YEAR,Integer.parseInt(year));
			cal.set(Calendar.MONTH, Integer.parseInt(month) -1 );
			cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
			boolean result = ApplicationFacade.getInstance().generateMonthlyBill(cal.getTime());
			if (result) {
				JOptionPane.showMessageDialog(this,
					"Bill Generated Successfully",
					"Bill Generated.",
					JOptionPane.INFORMATION_MESSAGE);
			}
			else {
				JOptionPane.showMessageDialog(this,
						"Bill was generated previously",
						"Duplicate bill exist.",
						JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	
	public void clear() {
		comboBoxMonth.setSelectedIndex(0);
		comboBoxYear.setSelectedIndex(0);
		monthlyBillComboBoxMonth.setSelectedIndex(0);
		monthlyBillComboBoxYear.setSelectedIndex(0);
		txtBill.setText("");
		txtAccountNo.setText("");
	}
	
	public void doUpdate() {
		clear();
		//Update the Date/Time Combo Box
	}

	
	public static ViewBillPanel getInstance() {
		synchronized (ViewBillPanel.class) {
			if (INSTANCE == null) {
				INSTANCE = new ViewBillPanel();
			}
			return INSTANCE;
		}
	}

	public void setAdminFunction(boolean admin) {
		monthlyBillGenerate.setEnabled(admin);
	}
}
